<?php

namespace App\Http\Controllers\Settle;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Chanjet\TplusCommonController;
use App\Http\Validate\Settle\SettleType as SettleSettleType;
use App\Jobs\Basic;
use App\Models\Settle\SettleType;
use App\Services\Chanjet\ChanjetRequest;
use Illuminate\Http\Request;

/**
 * 结算方式相关
 */
class SettleTypeController extends BaseController
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        //需获取当前用户所属账套
        $status = $request->status ?? 0;
        $isgetall = $request->isgetall ?? 0;
        $list = SettleType::getList($request, $this->mangerinfo['cpid'], $status, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['type_no', 'type_name', 'bank_no']);

        $Validate = new SettleSettleType();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $isexist = SettleType::where([['type_no', $req_data['type_no']],['cpid' , $this->mangerinfo['cpid']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '编码名称不允许重复']);
        }

        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'type_no' => $req_data['type_no'],
            'type_name' => $req_data['type_name'],
            'bank_no' => $req_data['bank_no'],
            'update_at' => time()
        ];
        $data['create_at'] = time();
        SettleType::create($data)->id;

        $ChanjetParams = [
            "param" => [
                "BankAccountType"=>"OTHER",
                "BankAccountNo"=> $req_data['bank_no'],
                "Code"=>$req_data['type_no'],
                "Name"=>$req_data['type_name'],
            ]
        ];
        $requestData = [
            "api" => "BankAccountCallBack",
            "param" => $ChanjetParams,
            "mangerinfo" => $this->mangerinfo
        ];
        Basic::dispatch($requestData);

        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $id = $request->id ?? 0;
        $info = SettleType::where([['id', $id], ['cpid', $this->mangerinfo['cpid']],['status',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $req_data = $request->only(['type_no', 'type_name', 'bank_no']);

        $Validate = new SettleSettleType();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $isexist = SettleType::where([['id','<>', $id], ['type_no', $req_data['type_no']],['cpid' , $this->mangerinfo['cpid']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '编码名称不允许重复']);
        }

        $data = [
            'type_no' => $req_data['type_no'],
            'type_name' => $req_data['type_name'],
            'bank_no' => $req_data['bank_no'],
            'update_at' => time()
        ];
        SettleType::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->update($data);

        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {

        $id = $request->id ?? 0;
        $info = SettleType::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        SettleType::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
